﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace BillGuru
{
    public partial class FrmPayment : Form
    {
        public FrmPayment()
        {
            InitializeComponent();
        }

        private void paymentBindingNavigatorSaveItem_Click(object sender, EventArgs e)
        {
            SavePayment();
        }

        private void SavePayment()
        {
            this.Validate();
            this.paymentBindingSource.EndEdit();
            this.tableAdapterManager.UpdateAll(this.billGuruDataSet);
        }

        private void FrmPayment_Load(object sender, EventArgs e)
        {
            this.paymentTypeTableAdapter.Fill(billGuruDataSet.PaymentType);
            this.customerTableAdapter.Fill(billGuruDataSet.Customer);

            this.paymentTableAdapter.Adapter.RowUpdated += new System.Data.OleDb.OleDbRowUpdatedEventHandler(Adapter_RowUpdated);
        }

        void Adapter_RowUpdated(object sender, System.Data.OleDb.OleDbRowUpdatedEventArgs e)
        {
            BillGuru.Persistence.SharedEventHandlerImpl.RetrieveKeyOnRowInsert(sender, e);
        }

        private void pbSave_Click(object sender, EventArgs e)
        {
            SavePayment();
        }

        private void pbSearch_Click(object sender, EventArgs e)
        {
            paymentTableAdapter.Adapter.SelectCommand = new System.Data.OleDb.OleDbCommand();
            string sCmdText =
                 "Select * from Payment where PaymentDate between #" + dtpStartDate.Value.ToShortDateString()
                 + "# and #" + dtpEndDate.Value.ToShortDateString() + "#";
            if (ckCustomer.Checked)
            {
                sCmdText += " And CustomerID = " + cbCustomer.SelectedValue;
            }

            paymentTableAdapter.Adapter.SelectCommand.CommandText = sCmdText;

            paymentTableAdapter.Adapter.SelectCommand.Connection = new System.Data.OleDb.OleDbConnection();
            paymentTableAdapter.Adapter.SelectCommand.Connection.ConnectionString =
                 BillGuru.Properties.Settings.Default.BillGuruConnectionString;

            billGuruDataSet.Payment.Clear();
            paymentTableAdapter.Adapter.Fill(billGuruDataSet.Payment);
        }

        private void pbDone_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FrmPayment_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this.billGuruDataSet.HasChanges())
                BillGuru.UI.PermissionHandler.checkDataLoss(e);
        }
    }
}
